package jp.ne.hatena.d.digitalsoul.loansyndicate.roles

import jp.ne.hatena.d.digitalsoul.loansyndicate.models.SharePie
import jp.ne.hatena.d.digitalsoul.loansyndicate.models.Share

trait AmountPie extends SharePie {
	
	def increase(amount:BigInt, criterion:SharePie) = 
		allotToEachShare(amount, criterion, (share:Share, allot:BigInt) => share.amount += allot)
	
	def decrease(amount:BigInt, criterion:SharePie) = 
		allotToEachShare(amount, criterion, (share:Share, allot:BigInt) => share.amount -= allot)
	
	private def allotToEachShare(amount:BigInt, criterion:SharePie, operate:(Share, BigInt) => Unit) = 
		for {
            thatShare:Share <- criterion.shares.values
	    	currentTotalAmount:BigInt = criterion.totalAmount
		} operate(shares(thatShare.ownerId), criterion.allot(thatShare.ownerId, amount, currentTotalAmount))
}